package com.xiaoy.util.store;

import java.util.HashMap;
import java.util.Map;

import com.xiaoy.util.store.io.ReadFileUtil;

/**
 * 读取配置文件的信息
 * @author chenty
 * 创建时间 2012-5-13
 */
public class VariableStore {

	public static Map<String ,String> vs = new HashMap<String, String>();
	
	private VariableExpander expander = null;
	
	private VariableStore () {
//		InputStream is = VariableStore.class.getResourceAsStream("/global.properties");
//		Properties props = new Properties();
//		try {
//			props.load(is);
//			Iterator<Object> it = props.keySet().iterator();
//			while (it.hasNext()){
//				String key = (String)it.next();
//				vs.put(key,props.getProperty(key));
//			}
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
		
//		vs.putAll(ReadFileUtil.readProperties("/global"));
		
		
		vs.putAll(ReadFileUtil.readDirProperties("config"));
		expander = new VariableExpander (this,"${","}") ;
	}
	
	private static VariableStore variableStore = new VariableStore();
	
	public static VariableStore getInstance () {
		return variableStore;
	}
	
	public static void setValue (String key,String value) {
		
		vs.put(key, value);
	}
	
	/**
	 * 得到配置文件中的字符值
	 * @param key
	 * @return
	 */
	public static String getValue(String key){
		
		return variableStore.getVariableValue(key);
	}
	
	public String getVariableValue(String key){
		
		
		Object obj = vs.get(key);
		if(obj == null){
			return "";
		}
		return expander.expandVariables(obj.toString());
	}
	
	/**
	 * 得到配置文件中的数字值
	 * @param key
	 * @return
	 */
	public static int getIntValue(String key){
		
		Object obj = vs.get(key);
		if(obj != null){
			return Integer.parseInt(obj.toString());
		}
		return -1;
	}
	
}
